package com.hlt.controller.user;

import com.hlt.dto.FavoritesDTO;
import com.hlt.dto.FavoritesPageQueryDTO;
import com.hlt.result.PageResult;
import com.hlt.result.Result;
import com.hlt.service.IFavoritesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lzf
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/users/favorites")
@Api(tags = "收藏接口")
@Slf4j
public class FavoritesController {

    @Resource
    private IFavoritesService favoritesService;

    @PostMapping
    @ApiOperation("收藏文章")
    public Result addFavorite(@RequestBody FavoritesDTO favoritesDTO) {
        log.info("收藏文章：{}", favoritesDTO);
        favoritesService.addFavorite(favoritesDTO);
        return Result.success();
    }

    @DeleteMapping("/{postId}")
    @ApiOperation("取消收藏")
    public Result removeFavorite(@PathVariable Integer postId) {
        log.info("取消收藏文章：{}", postId);
        favoritesService.removeFavorite(postId);
        return Result.success();
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("获取用户的收藏列表")
    public Result<PageResult> getUserFavorites(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取用户收藏列表，用户ID：{}", userId);
        FavoritesPageQueryDTO pageQueryDTO = new FavoritesPageQueryDTO();
        pageQueryDTO.setUserId(userId);
        pageQueryDTO.setPageNum(pageNum);
        pageQueryDTO.setPageSize(pageSize);
        PageResult pageResult = favoritesService.getUserFavorites(pageQueryDTO);
        return Result.success(pageResult);
    }
}
